package com.dragon.client.controller.wechat;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.common.model.Insert;
import com.dragon.common.model.ResponseData;
import com.dragon.common.model.Update;
import com.dragon.logical.service.wechat.IWechatArticleService;
import com.dragon.repository.domain.dto.WechatArticleDTO;
import com.dragon.repository.domain.vo.WechatArticleVO;
import com.dragon.repository.entity.WechatArticle;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * <p>
 * 微信文章发布表 前端控制器
 * </p>
 *
 * @author sy
 * @since 2023-11-30
 */
@RestController
@RequestMapping("article")
@Api(tags = "微信文章管理")
public class WechatArticleController {


    @Autowired
    IWechatArticleService service;



    @GetMapping("list")
    @ApiOperation(value = "获取文章列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title",required = false,type = "string",value = "名称"),
            @ApiImplicitParam(name = "status",required = false,type = "int",value = "状态"),
            @ApiImplicitParam(name = "page",required = true,type = "int",value = "页码"),
            @ApiImplicitParam(name = "limit",required = true,type = "int",value = "条数"),
    })
    public ResponseData selectArticleList(
            @RequestParam(required = false) String title, @RequestParam(required = false) Integer status,
            @RequestParam Integer page,@RequestParam Integer limit){
        Page<WechatArticle> pageResult = service.selectArticle(title, status, page, limit);
        return ResponseData.success(WechatArticleDTO.convertList(pageResult.getRecords()), pageResult.getTotal());
    }

    @GetMapping("getOne")
    @ApiOperation(value = "获取某个文章")
    public ResponseData getArticleOne(@ApiParam(name = "id", value = "id", required = true, type = "int")
                                      @RequestParam Integer id){
        return ResponseData.success(service.getArticleOne(id));
    }

    @PostMapping("add")
    @ApiOperation(value = "添加文章")
    public ResponseData addArticle(@RequestBody @Validated(Insert.class)WechatArticleVO vo){
        service.addArticle(WechatArticleVO.convert(vo));
        return ResponseData.success();
    }

    @PostMapping("update")
    @ApiOperation(value = "修改文章")
    public ResponseData updateArticle(@RequestBody @Validated(Update.class)WechatArticleVO vo){
        service.updateArticle(WechatArticleVO.convert(vo));
        return ResponseData.success();
    }

    @PostMapping("delete")
    @ApiOperation(value = "删除文章")
    public ResponseData deleteArticle(@ApiParam(name = "id", value = "id", required = true, type = "int")
                                      @RequestParam Integer id){
        service.deleteArticle(id);
        return ResponseData.success();
    }

    @PostMapping("publish")
    @ApiOperation(value = "发布文章")
    public ResponseData publishArticle(@ApiParam(name = "id", value = "id", required = true, type = "int")
                                      @RequestParam Integer id){
        service.releaseArticle(id);
        return ResponseData.success();
    }

    @GetMapping("getArticleStatus")
    @ApiOperation(value = "获取文章发布状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", required = true, type = "int",value = "id"),
            @ApiImplicitParam(name = "publishId", required = true, type = "int", value = "发布回调id"),
    })
    public ResponseData getArticleStatus(@RequestParam Integer id,
                                         @RequestParam String publishId){
        return ResponseData.success(service.getArticleStatus(id,publishId));
    }

    @PostMapping("upload")
    @ApiOperation(value = "上传文件")
    public ResponseData uploadFile(@RequestParam("file") MultipartFile file)throws IOException {
        return ResponseData.success(service.uploadFile(file));
    }


}
